package com.pzr.soccer.crawler.util;

import com.pzr.soccer.crawler.aop.EnableRecordError;
import com.pzr.soccer.crawler.model.CrawlerContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;


/**
 * 持有tag url信息，并处理爬取错误
 * @Author pzr
 * @date:2021-12-14-8:45
 * @Description:
 **/
@Component
@Slf4j
@Data
public class CrawlerHelper {
    public static String Parse_Errors_Occurred="parse_error_occurred";

    @EnableRecordError
    public  Element getBodyByJvppteer(CrawlerContext context){
        String content = JvppeteerHelper.getContent(context.getUrl());
        return parseToElement(content,context);
    }

    @EnableRecordError
    public  Element getBodyByJsoup(CrawlerContext context) {
        String content = JsoupHelper.getContent(context.getUrl());
        return parseToElement(content,context);

    }

    @EnableRecordError
    public  Element getBodyBySelenium(CrawlerContext context) {
        String content = SeleniumHelper.getContent(context.getUrl());
         return parseToElement(content,context);
    }


    public Element first(Element parent, CrawlerContext context){
        return elements(parent,context).first();
    }
    public Element last(Element parent, CrawlerContext context){
        return elements(parent,context).last();
    }

    @EnableRecordError
    public Elements elements(Element parent, CrawlerContext context){
        Elements els=parent.select(context.getSelector());
        if(els==null){
            context.setExtractErrors(context.getExtractErrors()+1);
            return new Elements(new Element("none elements"));
        }
        return els;
    }



    /**
     *
     *  如果以@GeneralConst  parse Errors Occurred 开头的返回值，表示解析页面出错
     * @param contentStr like @GeneralConst  parse Errors Occurred+Error message
     * @return jsoup node not null
     */
    private Element parseToElement(String contentStr, CrawlerContext context){
        if(contentStr.startsWith(Parse_Errors_Occurred)){
            context.setParseErrors(context.getParseErrors()+1);
            return new Element("none_body");
        }
        return Jsoup.parse(contentStr).body();
    }

}
